﻿Imports System.Activities
Imports System.Activities.Presentation.Metadata
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.ComponentModel

Public Class GetUserAgentStringsActivity
    Inherits CodeActivity
    Implements IRegisterMetadata

#Region "INet API"
    <DllImport("urlmon.dll", CharSet:=CharSet.Ansi)>
    Private Shared Function UrlMkGetSessionOption(
                                              ByVal dwOption As Integer, ByVal pBuffer As System.Text.StringBuilder,
                                              ByVal dwBufferLength As Integer, ByRef pdwBufferLength As Integer,
                                              ByVal dwReserved As Integer) As Integer
    End Function

    Private Const URLMON_OPTION_USERAGENT As Integer = &H10000001
#End Region

    Public Property Result As OutArgument(Of String)

    Public Sub New()
        Me.DisplayName = My.Resources.GetUserAgentStringsActivityDisplayName
    End Sub

    Protected Overrides Sub Execute(context As System.Activities.CodeActivityContext)

        Dim sb As New System.Text.StringBuilder(256)
        Dim resultLength = 0
        Try
            Dim res = UrlMkGetSessionOption(URLMON_OPTION_USERAGENT, sb, sb.Capacity, resultLength, 0)
        Catch ex As Exception

        End Try
        context.SetValue(Me.Result, sb.ToString)
    End Sub

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder
        'GetUserAgentStringsActivity Activity
        build.AddCustomAttributes(GetType(GetUserAgentStringsActivity),
                                              New DesignerAttribute(GetType(GetUserAgentStringsActivityDesigner)),
                                              New ToolboxBitmapAttribute(GetType(GetUserAgentStringsActivity), "GetUserAgentStringsActivity.png"))

        build.AddCustomAttributes(GetType(GetUserAgentStringsActivity), "DisplayName",
                                      New CategoryAttribute(My.Resources.CategorySetting),
                                      New DisplayNameAttribute(My.Resources.DisplayNameDisplay))
        build.AddCustomAttributes(GetType(GetUserAgentStringsActivity), "Result",
                                      New CategoryAttribute(My.Resources.CategoryDetail),
                                      New DisplayNameAttribute(My.Resources.ResultDisplay),
                                      New DescriptionAttribute(My.Resources.ResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub
End Class
